package com.clever.mall.service.impl;

import com.clever.mall.commons.dto.UserRequestParam;
import com.clever.mall.commons.dto.WithdrawDepositRequestParam;
import com.clever.mall.commons.exception.UserException;
import com.clever.mall.commons.result.WalletPage;
import com.clever.mall.entity.TradingRecord;
import com.clever.mall.entity.User;
import com.clever.mall.mapper.TradingRecordMapper;
import com.clever.mall.mapper.UserMapper;
import com.clever.mall.mapper.WalletMapper;
import com.clever.mall.service.WalletService;
import com.clever.mall.utils.MyBeanUtils;
import com.clever.mall.utils.StatusCode;
import com.clever.mall.utils.WalletServerConfig;
import com.clever.mall.vo.TradingRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @author ChenWang
 * @className WalletServiceImpl
 * @date 2020/11/10 10:06
 * @since JDK 1.8
 */
@Service
@Slf4j
public class WalletServiceImpl implements WalletService {
    @Resource
    UserMapper userMapper;
    @Resource
    WalletMapper walletMapper;
    @Resource
    TradingRecordMapper tradingRecordMapper;

    /**
     * 获取我的钱包信息
     *
     * @param userRequestParam 指定的用户
     * @return 钱包信息
     */
    @Override
    public WalletPage getMyWalletPage(UserRequestParam userRequestParam) {
        User user = new User();
        BeanUtils.copyProperties(userRequestParam, user);
        user = userMapper.selectUserByUser(user);
        /*如果用户不存在*/
        if (Objects.isNull(user)) {
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        /*查询用户的交易记录*/
        List<TradingRecord> tradingRecords = walletMapper.selectTradingRecordByUser(user);
        List<TradingRecordVo> tradingRecordVos = MyBeanUtils.copyListProperties(tradingRecords, TradingRecordVo::new);
        /*查询用户的余额信息*/
        User newUser = userMapper.selectUserByUser(user);
        /*设置对应的信息*/
        WalletPage walletPage = new WalletPage();
        walletPage.setUserId(newUser.getUserId());
        walletPage.setBalance(newUser.getBalance());
        /*个人的交易记录*/
        walletPage.setTradingRecordVos(tradingRecordVos);

        /*提现最小金额*/
        walletPage.setMinMoney(WalletServerConfig.MINMONEY);
        /*提现最大金额*/
        walletPage.setMaxMoney(WalletServerConfig.MAXMONEY);
        /*提现所需最小工作日*/
        walletPage.setAtLeastDay(WalletServerConfig.ATLEASTDAY);
        /*提现所需最大工作日*/
        walletPage.setAtTheMostDay(WalletServerConfig.ATTHEMOSTDAY);
        /*提现的相关的工作人员微信*/
        walletPage.setServerVechat(WalletServerConfig.SERVERVECHAT);
        return walletPage;
    }

    /**
     * 提现金额的服务
     *
     * @param withdrawDepositRequestParam 传入的指定的提现金额的参数
     * @return 0表示提现失败，1表示提现成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int withdrawDeposit(WithdrawDepositRequestParam withdrawDepositRequestParam) {
        if (checkTheAccount(withdrawDepositRequestParam.getName(), withdrawDepositRequestParam.getPhoneNumber()) == 0) {
            throw new UserException(StatusCode.USER_VECHAT_ERROR);
        }
        User user = new User();
        user.setUserId(withdrawDepositRequestParam.getUserId());
        user = userMapper.selectUserByUser(user);
        if(Objects.isNull(user)){
            throw new UserException(StatusCode.USER_NOT_EXIST_ERROR);
        }
        BigDecimal balance = user.getBalance();
        if(balance.compareTo(withdrawDepositRequestParam.getMoney())<0){
            throw new UserException(StatusCode.USER_NOT_SUFFICIENT_FUNDS);
        }
        /* 用户余额减去提现金额 */
        user.setBalance(balance.subtract(withdrawDepositRequestParam.getMoney()));
        /* 更新用户数据*/
        userMapper.updateUser(user);
        /*记录交易记录*/
        TradingRecord tradingRecord = new TradingRecord();
        tradingRecord.setUserId(withdrawDepositRequestParam.getUserId());
        tradingRecord.setMoney(withdrawDepositRequestParam.getMoney());
        /* 2表示提现 */
        tradingRecord.setType(2);
        /* 插入数据 */
        tradingRecordMapper.insertTradingRecord(tradingRecord);
        return 0;
    }

    /**
     * 模拟检测微信的账号是否正确
     *
     * @param username 微信支付认证的名字
     * @param phone    微信支付认证的手机号码
     * @return 0表示失败，1表示成功
     */
    private int checkTheAccount(String username, String phone) {
        log.info("调用第三方的接口");
        return 1;
    }


}
